home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Deutsche Edition 1
/
Deutsche Edition 1.iso
/
amok
/
amok_lha
/
amok15.lha
/
Seafarers_Manual
/
Source
/
Prime.mod
< prev
next >
Wrap
Text File
|
1993-08-15
|
1KB
|
49 lines
MODULE Prime; (* Generate prime numbers *)
(* From the book "Modula-2 A Seafarer's Manual and Shipyard Guide" *)
(* Page 73 adapted "Amiga M2Modula-2" 04 Mar 1988 *)
FROM InOut IMPORT WriteLn,
WriteCard,
WriteString;
CONST
FlgSize = 8190; (* array size *)
VAR
Flags : ARRAY [0..FlgSize] OF BOOLEAN;
(* indicates primes
if nth element = TRUE, then n + n + 3 is prime
if nth element = FALSE, then n + n + 3 is not prime
(n ranges from 0 to 8190) *)
i, (* indexes for *)
k, (* Flags array *)
Prime, (* prime number *)
Count : CARDINAL; (* number of primes found *)
BEGIN
Count := 0; (* initialize num. found *)
FOR i := 0 TO FlgSize BY 1 DO (* initialize array as *)
Flags[i] := TRUE;
END;
FOR i := 0 TO FlgSize BY 1 DO
IF Flags[i] THEN (* prime? *)
Prime := i + i + 3; (* yes-calculate next one *)
WriteCard (Prime,7); (* display prime *)
k := i + Prime; (* index to multiple *)
WHILE (k <= FlgSize) DO (* indicate multiple nonprime *)
Flags[k] := FALSE;
INC(k,Prime); (* increment to next nonprime *)
END; (* WHILE *)
INC(Count);
END; (* IF *)
END; (* FOR *)
WriteLn;
WriteCard (Count,6);
WriteString (" primes");
WriteLn;
END Prime.